/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2023-08-23
 * Time: 9:38
 */
public class MyQueue {
    public static class ListNode{
        public int value;
        ListNode next;
        ListNode prev;

        public ListNode(int value) {
            this.value = value;
        }
    }

    ListNode first;
    ListNode last;
    int size=0;



    //入队列，从队尾插入元素
    public void offer(int data){
        ListNode newNode=new ListNode(data);
        if(first==null){//队列为空
            first=newNode;
            last=newNode;
        }else{//一般情况
            last.next=newNode;
            newNode.prev=last;
            last=newNode;
        }
        size++;
    }

    //出队列，删除双向链表的第一个节点
    //一般情况
    public int poll(){
        int val=0;
        if(first==null){
            throw new NullQueueException("出队列时，队列为空");
        }
        if(first.next==null){
            val=first.value;
            first.prev=null;
            first.next=null;
            size--;
            return val;
        }else{
            val=first.value;
            first.next.prev=null;
            first=first.next;
            first.prev=null;//将前面一个节点置空
            size--;
            return val;
        }
    }

    //获取队列头部的第一个元素
    public  int peek(){
        if(first==null){
            throw  new NullQueueException("获取队头元素时，队列为空");
        }
        return first.value;
    }
    //获取队列中元素的个数
    public int size(){
        return size;
    }
    //判断队列是否为空
    public boolean isEmpty(){
        return first==null;
    }

    public static void main(String[] args) {
        MyQueue myQueue1=new MyQueue();
        myQueue1.offer(23);
        myQueue1.offer(34);
        myQueue1.offer(3);
        myQueue1.offer(2);

        System.out.println(myQueue1.poll());
        System.out.println(myQueue1.poll());
        System.out.println(myQueue1.peek());

        System.out.println(myQueue1.size);
        System.out.println(myQueue1.isEmpty());
    }

}
